package main

import (
	_ "testapp/routers"
	_ "github.com/go-sql-driver/mysql"

	"github.com/astaxie/beego"
	"net"
	"errors"
	"fmt"
	"path/filepath"
	"os"
)

func main() {
	//1.有ORM对象
	//o := orm.NewOrm()
	//orm.Debug = true
	//2.查询的对象
	//user := User{}
	//user.Name = "goname1"
	//user.Password = "gopassword1"
	//id,_:=o.Insert(&user)
	//fmt.Printf("insert id is %d", id)

	//3.指定查询对象字段值
	//user.Id = 3
	//4.查询-只有Id是参数可以省略
	//err := o.Read(&user)




	//builder, err := orm.NewQueryBuilder("mysql")
	//if err != nil {
	//	fmt.Printf(err.Error())
	//}
	//builder.Select("*").From("user").Where("name = 'goname'")
	//rawSeter:=o.Raw(builder.String())
	//var users []User
	//rawSeter.QueryRows(&users)
	//fmt.Printf("size is %d", len(users))
	//for  i := 0; i< len(users); i++  {
	//	fmt.Printf("the %d element is %s", i, users[i].Name)
	//}

	//o.Raw(builder.Select("*").From("user").Where("id = 1").String()).QueryRows(users)




	//user.Name = "updateName"
	//user.Password = "updatePass"
	//id, err := o.Update(&user)

	//u := &User{}
	//u.Id = 1
	//err = o.Read(u, "UserName")


	//id, err := o.Delete(&user)

	//err := o.Read(user, "name", "id")


	//if err != nil {
	//	fmt.Printf("select error ",err)
	//}
	//fmt.Printf(user.Name + " id:is %d",id)


	//beego.SetStaticPath("/static","public")



	//fmt.Sprintf("http://%S:8081",  LocalIPv4())

	//打包可执行文件时跳转工作目录
	Chdir()

	beego.SetStaticPath("/files","files")

	ip, _ := LocalIPv4()
	fmt.Printf("request sample:\nhttp://%s:8090/files\n", ip)

	fmt.Printf("\nrequest sample:\nhttps://%s:8100/files\n\n", ip)

	beego.Run() //运行beego服务


}

func LocalIPv4() (string, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return "", err
	}
	for _, iface := range ifaces {
		if iface.Flags&net.FlagUp == 0 {
			continue
		}
		if iface.Flags&net.FlagLoopback != 0 {
			continue
		}
		addrs, err := iface.Addrs()
		if err != nil {
			return "", err
		}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			if ip == nil || ip.IsLoopback() {
				continue
			}
			ip = ip.To4()
			if ip == nil {
				continue
			}
			return ip.String(), nil
		}
	}
	return "", errors.New("未连接网络")
}

// Chdir 将程序工作路径修改成程序所在位置
func Chdir() (err error) {
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		return
	}

	err = os.Chdir(dir)
	return
}


//func (this User) ToString() string {
//	return fmt.Sprintf("Name:%s\tEmail:%s\t", this.name, this.password)
//}





//
//func init() {
//	//设置数据库基本信息
//	orm.RegisterDataBase("default", "mysql", "root:cuiweixing@tcp(127.0.0.1:3306)/david?charset=utf8")
//	//映射model数据
//	orm.RegisterModel(new(User))
//	//生成表
//	orm.RunSyncdb("default", false, true)
//
//	fmt.Printf("init is called")
//
//	//orm.RegisterDataBase("default", "mysql", "test:@/test?charset=utf8")//密码为空格式
//}
//
//
//
//
//
//
//
//func multiThread(){
//	//var string1 = "hello"
//	//var string2 = "world"
//	printString("hello")
//	go printString("world")
//}
//
//
//func max(a int, b int)int{
//	return a+b
//}
//
//func arrayTest(){
//	var myArray = [...]int{1, 2, 3}
//	var myArray1 = [3]int{3, 2, 1}
//
//	var address *int = &myArray[1]
//	fmt.Printf("array first element is %d\n",myArray[0])
//	print(myArray1[0])
//	fmt.Printf("array[1] adress is %d\n",address)
//
//	//var user1 User
//	//user1.name = "haha"
//	//user1.password = "10"
//	//fmt.Printf("user1's name is %s \n", user1.name)
//
//	var splice1 = []int{1,2,3}
//	fmt.Printf("splice1's first element is %d\n", splice1[0])
//	fmt.Printf("splice1's first and second element is %d\n", splice1[0:2])
//
//	for i, num :=range splice1 {
//		fmt.Printf("element %d:%d\n", i, num)
//	}
//
//	var map1 map[string]string = make(map[string]string)
//	map1["key1"] = "value1"
//	map1["key2"] = "value2"
//	map1["key3"] = "value3"
//	for k,v:=range map1  {
//		fmt.Printf("map1's element is %s:%s\n", k, v)
//	}
//	delete(map1, "key1")
//
//	fmt.Printf("conculateMute 5 is %d", conculateMulteNumber(5))
//}
//
//type User struct {
//	Id       int `orm:"column(id);pk"`
//	Name     string `orm:"column(name);"`
//	Password string `orm:"column(password);"`
//}
//
//func conculateMulteNumber(number int)int{
//	if number <= 1 {
//		return 1
//	} else {
//		return number*conculateMulteNumber(number - 1)
//	}
//}
//
//func printString(s string){
//	for i := 0; i < 5; i++{
//		time.Sleep(100 * time.Millisecond)
//		fmt.Printf("current item is %s\n",s)
//	}
//}